package com.yushu.common.config;

import com.yushu.common.config.cache.ShiroCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
@Configuration
public class CacheConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * SpringCacheManager 缓存管理器
     *     使用的缓存产品是Redis
     * @return
     */
    @Bean(name = "springCacheManager")
    public RedisCacheManager springCacheManager(){
        // 创建一个Redis缓存的默认配置
        RedisCacheConfiguration conf = RedisCacheConfiguration.defaultCacheConfig();
        // 配置序列化的规则
//        conf = conf.entryTtl(Duration.ofSeconds(-1));//设置缓存的默认过期时间
        RedisCacheManager cacheManager = RedisCacheManager
                .builder(redisConnectionFactory)
                .cacheDefaults(conf)
                .build();
        return cacheManager;
    }

    /**
     * shiro 的缓存管理器
     * @return
     */
    @Bean(name = "shiroCacheManager")
    public ShiroCacheManager shiroCacheManager(){
        // 创建一个Redis缓存的默认配置
        RedisCacheConfiguration conf = RedisCacheConfiguration.defaultCacheConfig();
        // 设置会话的有效期
//        conf = conf.entryTtl(Duration.ofSeconds(30));
        RedisCacheManager cacheManager = RedisCacheManager
                .builder(redisConnectionFactory)
                .cacheDefaults(conf)
                .build();
        return new ShiroCacheManager(cacheManager);
    }
}
